<?php 
		
		/**
		* 翻页函数封装
		* $parem link 数据库链接对象
		* $parem table 表名
		* $parem cur_page 当前页
		* $parem per_page 每页显示的记录数
		* $parem page_num 固定页码数
		* @return  string
		* @author  1zh 170515
		 **/

		 function getPages($cur_page=1,$per_page=3,$page_num=5){

		 	//数据库链接
			
			$link = mysqli_connect('127.0.0.1','root','root','p802');
   
			    if(!$link){

	        echo '<script>alert("数据库链接失败");history.go(-1);</script>';die;
	        }


			//求数据表总记录数
		 	$sql = "select count(*) as total from `news`";
		 	$result = mysqli_query($link,$sql);

		 	$total_num = mysqli_fetch_assoc($result)['total'];

		 	$cur_page = isset($_GET['p'])?$_GET['p']:1;

			if($cur_page <1) {
			 $cur_page  =1;
			}
		 //求总页面数并向上取整
		 $total_page = ceil($total_num/$per_page);

		 //求访问a标签页码的前后偏移副 公式=固定页码数/2向下取整
		 $offset_page = floor($page_num/2);

		 //计算循环的开始值 当前访问的页面-前后偏移副
		 $star = $cur_page - $offset_page; //6-2    4
		 $star = $star<1 ? 1 : $star;
		 //循环的结束值    当前访问的页码+前后偏移副
		 $end = $cur_page + $offset_page;
		 $end = $end>$total_page? $total_page : $end;


		  //当前显示的页码个数不够最大页码数，在进行左右调整
		  $curPageNum = $end-$star+1;
		  //左调整
		  if($curPageNum<$page_num && $star>1){
		   $star = $star - ($page_num-$curPageNum);
		   $star = $star<1 ? 1 : $star;
		   $curPageNum = $end-$star+1;
		  }
		  //右边调整
		  if($curPageNum<$page_num && $end<$total_page){
		   $end = $end + ($page_num-$curPageNum);
		   $end = $end>$total_page? $total_page : $end;
		  }

		 $pages = '';


		 //首页
		 $disabled =$cur_page==1?'disabled':'';

		 $href =$cur_page==1?'#':'index.php?c=news&a=list&admin=1&p=1';

		 $pages.='<ul class="pagination"><li class="'.$disabled.'"><a href="'.$href.'" tabindex="0" class="paginnate_button first">首页</a></li>';

		 //上一页
		 $prev = $cur_page-1;
		 $prev_href = $cur_page==1?'#':'index.php?c=news&a=list&admin=1&p='.$prev;
		 $pages.='<li class="'.$disabled.'"><a href="'.$prev_href.'" tabindex="0" class="paginate_button previous" id="datatable1_previous">上一页</a></li>';


		 //动态生成中间5页页码
		 for($i=$star;$i<=$end;$i++){

		 	//<!-- 判断当前页面是否属于循环的$i值，是则显示选中状态的li标签 -->
		 	if($i==$cur_page){
                  
		 		$pages .='<li class="active"><a tabindex="0">'.$i.'</a></li>';
		 	}else{
		 		$pages.='<li><a href="index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1&p='.$i.'" tabindex="0">'.$i.'</a></li>';
		 	}
		 }

		  //下一页
		 $disabled = $cur_page==$total_page?'disabled':'';
		 $next = $cur_page+1;
		 $next_href = $cur_page==$total_page?'#':'index.php?c=news&a=list&admin=1&p='.$next;

		 $pages.='<li class="'.$disabled.'"><a href="'.$next_href.'" tabindex="0" class="paginate_button next" id="datatable1_next">下一页</a</li>';

		 //尾页
		 $last_href = $cur_page==$total_page?'#':'index.php?c=news&a=list&admin=1&p='.$total_page;
		 $pages.='<li class="'.$disabled.'"><a href="'.$last_href.'" tabindex="0" class="paginate_button last" id="datatable1_last">尾页</a></li>

									 </ul>';

		 return $pages;
 
		}